// pages/myfavorite/index.js
const app = getApp() // 引入app
import _request from '../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    marginBottom: app.globalData.marginBottom,
    imgUrl: app.globalData.imgUrl, //全局img路径
    shoppage: 1,
    shoplist: [],
    hairpage: 1,
    hairlist: [],
    dynamicpage: 1,
    dynamiclist: [],
  },

  // 查看地图
  seeMap(e) {
  const {
    latitude,
    longitude,
    location
  } = e.currentTarget.dataset.item
  let lat = latitude;
  let lon = longitude;
  let bankName = location;
  wx.openLocation({
    latitude: lat,
    longitude: lon,
    name: bankName,
    scale: 18
  })
},

  // 前往美发师作品
  toHairstylistproduction(e) {
    const {
      item
    } = e.currentTarget.dataset
    item.active = 1
    let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/hairstylistdetails/index?hair=' + encodeURIComponent(obj),
    })
  },

  // 查看作品详情
  onDetailex(e) {
    const {
      item
    } = e.currentTarget.dataset
    // let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/workdetails/index?id=' + item.opusId,
    })
  },

  // 前往门店详情
  toShopdetails(e) {
    const {
      item
    } = e.currentTarget.dataset
    let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/shopdetails/index?shop=' + obj,
    })
  },

  // 前往美发师详情
  toHairstylistdetails(e) {
    const {
      item
    } = e.currentTarget.dataset
    let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/hairstylistdetails/index?hair=' + encodeURIComponent(obj),
    })
  },

  // 门店收藏
  getshop(e, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.get('/collection/getStore', {
      page: e,
      size: 10,
      status: 1,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      let list = res.data.data.map(item => {
        let distance = (parseInt(item.distance) / 1000).toFixed(2) * 1000;
        let newdistance = (Math.round(distance * 10) / 10000).toFixed(1)
        return {
          ...item,
          newdistance
        }
      })
      this.setData({
        shoplist: e > 1 ? [...this.data.shoplist, ...list] : list
      })
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error, 'error');
    })
  },

  // 美发师收藏
  gethair(e, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.get('/collection/getStylist', {
      page: e,
      size: 10,
      status: 1,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      let list = []
      if (res.data.data != undefined) {
        list = res.data.data.map(item => {
          let distance = (parseInt(item.distance) / 1000).toFixed(2) * 1000;
          let newdistance = (Math.round(distance * 10) / 10000).toFixed(1)
          if (item.lable) {
            let money = item.lable.match(/\d+(.\d+)?/g).join('');
            let meal = item.lable.split(money)[0]
            return {
              ...item,
              money,
              meal,
              newdistance
            }
          } else {
            return {
              ...item,
              newdistance
            }
          }
        })
      }
      this.setData({
        hairlist: e < 2 ? list : [...this.data.hairlist, ...list]
      })
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error, 'error');
    })
  },

  // 动态收藏
  getdynamic(e, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.get('/dynamicState/getCollectDynamicState', {
      page: e,
      size: 10,
      status: 1,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        dynamiclist: e > 1 ? [...this.data.dynamiclist, ...res.data.data] : res.data.data
      })
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error, 'error');
    })
  },

  // 点击取消收藏
  onDeletesite(e) {
    wx.showModal({
      title: '请确认是否要取消收藏',
      success: (res) => {
        if (res.confirm) {
          this.Deletesite(e)
        } else if (res.cancel) {}
      },
    })
  },

  // 取消收藏
  Deletesite(e) {
    let _this = this
    const {
      id,
      type
    } = e.currentTarget.dataset
    if (type == 0) {
      _request.postObj('/storeManage/updateCollectionType', {
        isCollection: 1,
        storeId: id,
        userId: wx.getStorageSync('userid')
      }).then((res) => {
        wx.showToast({
          title: '取消收藏成功',
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          _this.getshop(1, false)
        }, 500);
      }).catch((error) => {
        console.log(error, 'error');
      })
    } else if (type == 1) {
      _request.postObj('/store/storeCollection', {
        stylistId: id,
        type: 0,
        userId: wx.getStorageSync('userid')
      }).then((res) => {
        wx.showToast({
          title: '取消收藏成功',
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          _this.gethair(1, false)
        }, 500);
      }).catch((error) => {
        console.log(error, 'error');
      })

    } else if (type == 2) {
      _request.postObj('/storeStylist/oupsCollection', {
        opusId: id,
        type: 0,
        userId: wx.getStorageSync('userid')
      }).then((res) => {
        wx.showToast({
          title: '取消收藏成功',
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          this.getdynamic(1, false)
        }, 500);
      }).catch((error) => {
        console.log(error, 'error');
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getshop(this.data.shoppage, false)
    this.gethair(this.data.hairpage, false)
    this.getdynamic(this.data.dynamicpage, false)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.getshop(1, false)
    // this.gethair(1, false)
    // this.getdynamic(1, false)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})